package com.jp.knowledge.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.jiayen.util.DisplayUtil;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.FoundAdapter;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.model.FoundAppData;
import com.jp.knowledge.model.FundAppHome;
import com.jp.knowledge.view.FoundGridItem;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/15.
 */

public class FoundAllActivity extends SlidingActivity implements View.OnClickListener {

    @ViewInject(R.id.main_layout)
    private LinearLayout mainLayout;
    @ViewInject(R.id.app_view)
    private RecyclerView appView;

    private List<FundAppHome> foundAppDatas;
    private FoundAdapter adapter;

    @Override
    protected int getResourceId() {
        return R.layout.activity_found_all;
    }

    @Override
    protected void init() {
        initTitle();
        foundAppDatas = (List<FundAppHome>) getIntent().getSerializableExtra("foundAppDatas");
//        setAppData(foundAppDatas);

        if(foundAppDatas == null || foundAppDatas.size() == 0)
            return;
        List<FoundAppData> foundAppDataList = new ArrayList<>();
        for(FundAppHome foundAppData : foundAppDatas){
            List<FoundAppData> gropuApps = foundAppData.getGropuApps();
            if(gropuApps == null || gropuApps.size() == 0)
                continue;
            for(FoundAppData appData : gropuApps)
                if(appData.getIndexSort() != 10)
                    foundAppDataList.add(appData);
        }

        appView.setHasFixedSize(true);
        appView.setNestedScrollingEnabled(false);
        appView.setLayoutManager(new GridLayoutManager(mContext, 4));
        appView.addItemDecoration(new JpDiver(mContext));
        adapter = new FoundAdapter(mContext, foundAppDataList);
        appView.setAdapter(adapter);

        adapter.setOnItemListener(new OnItemListerner() {
            @Override
            public void onItemClick(View view, int position) {
                FoundAppData appData = (FoundAppData) adapter.getData().get(position);
                String code = appData.getCode();
                if(!TextUtils.isEmpty(code))
                    JpApplicationWebActivity.gotoWebActivity(mContext, code);
                else
                    ToasUtil.toast(mContext, "抱歉，路径为空");
            }
        });
    }

    private void initTitle(){
        topName.setText("发现");
        topDesc.setText("为你连接一切");

        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
        rightIcon.setVisibility(View.GONE);
        rightIcon.setOnClickListener(this);
    }

    private void setAppData(List<FundAppHome> list){
        mainLayout.removeAllViews();
        if(list == null)
            return;
        for(int i = 0, j = list.size(); i < j; i++){
            FoundGridItem gridItem = new FoundGridItem(mContext);
            gridItem.setHomeData(list.get(i));
            mainLayout.addView(gridItem);
            if(i < j-1)
                mainLayout.addView(getLineView());
        }
    }

    private View getLineView(){
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(mContext,10)));
        view.setBackgroundResource(R.color.found_item_line);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.icon_serach)
            startActivity(new Intent(mContext, SearchActivity.class));
        else if(v.getId() == R.id.icon_left)
            finish();
    }
}
